unit PesquisaNotaFiscal;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, UFormPesquisa, DB, DBTables, StdCtrls, Grids, DBGrids, Buttons,
  ExtCtrls, CheckLst, DBCtrls;

type
  TFPesquisaNotaFiscal = class(TFormPesquisa)
    CkBTodasEmpresas: TCheckBox;
    Label10: TLabel;
    CLBEmpresas: TCheckListBox;
    Label2: TLabel;
    DBLCBFornecedor: TDBLookupComboBox;
    DSFornecedor: TDataSource;
    ECodigo: TEdit;
    Label1: TLabel;
    ENotaFiscal: TEdit;
    Label4: TLabel;
    ESerie: TEdit;
    Label5: TLabel;
    RGSituacao: TRadioGroup;
    procedure BtnConsultarClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure CkBTodasEmpresasClick(Sender: TObject);
    procedure BtnLimparClick(Sender: TObject);
    procedure BtnSelecionarClick(Sender: TObject);
    procedure ENotaFiscalKeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  FPesquisaNotaFiscal: TFPesquisaNotaFiscal;
  s : String;

implementation

uses UGeral, UDmGestao, CadastroNotaFiscal;
{$R *.dfm}

procedure TFPesquisaNotaFiscal.BtnConsultarClick(Sender: TObject);
begin
 if (EmpresasDesmarcadas(CLBEmpresas) = CLBEmpresas.Items.Count) then
  CkBTodasEmpresasClick(Sender);
 S:=Concatenaempresas(CLBEmpresas);
 QPesquisa.Close;
 QPesquisa.SQL.Clear;
 QPesquisa.SQL.Add(' Select NF_Codigo,NF_Pedido,NF_Notafiscal ,NF_Serie,NF_Situacao,NF_DataEmissao,NF_DataEstoque,NF_TotalItens,NF_ValorNF from NotaFiscal ');
 QPesquisa.SQL.Add(' Where  (Codigoempresa in ( ' + s + ') )');
 if ECodigo.Text <> '' then
  begin
   QPesquisa.SQL.Add('and (NF_Codigo = :Cod) ');
   QPesquisa.ParamByName('Cod').AsString:=ECodigo.Text;
  end;
 if DBLCBFornecedor.Text <> '' then
  begin
   QPesquisa.SQL.Add('and (NF_Fornecedor = :Forn) ');
   QPesquisa.ParamByName('Forn').AsInteger:=DBLCBFornecedor.KeyValue;
  end;
 if RGSituacao.ItemIndex <> 3 then
  begin
   QPesquisa.SQL.Add('and (NF_Situacao = :Sit) ');
   QPesquisa.ParamByName('Sit').AsInteger:=RGSituacao.ItemIndex;
  end;
 if ENotaFiscal.Text <> '' then
  begin
   QPesquisa.SQL.Add('and (NF_NotaFiscal = :NF) ');
   QPesquisa.ParamByName('NF').AsString:=ENotaFiscal.Text;
  end;
 if ESerie.Text <> '' then
  begin
   QPesquisa.SQL.Add('and (NF_Serie = :Ser) ');
   QPesquisa.ParamByName('Ser').AsString:=ESerie.Text;
  end;
 QPesquisa.Open;
  inherited;
  BtnImprimir.Enabled:=false;
end;

procedure TFPesquisaNotaFiscal.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
 FCadastroNotaFiscal.Enabled:=true;
 FCadastroNotaFiscal.Show;
 FPesquisaNotaFiscal:=nil;
 inherited;
end;

procedure TFPesquisaNotaFiscal.FormCreate(Sender: TObject);
begin
  inherited;
 QPesquisa.Close;
 QPesquisa.SQL.Clear;
 QPesquisa.SQL.Add('Select Emp_Codigo,Emp_Fantasia from empresa');
 QPesquisa.Open;
 QPesquisa.First;
 CLBEmpresas.Items.Clear;
 while not(QPesquisa.Eof) do
 begin
  CLBEmpresas.Items.Add(QPesquisa.fieldByname('Emp_Codigo').AsString + '-' + QPesquisa.fieldByname('Emp_Fantasia').AsString );
  QPesquisa.Next;
 end;
 CkBTodasEmpresasClick(Sender);
 QPesquisa.Close;
end;

procedure TFPesquisaNotaFiscal.CkBTodasEmpresasClick(Sender: TObject);
Var
 i :Integer;
begin
  inherited;
 for i := 0 to CLBEmpresas.Items.Count - 1 do
  begin
   if StrToInt(Copy(CLBEmpresas.Items.Strings[i],1,pos('-',CLBEmpresas.Items.Strings[i])- 1)) = Codigoempresa then
    CLBEmpresas.Checked[i]:=true
   else
    CLBEmpresas.Checked[i]:=CkBTodasEmpresas.Checked;
  end;
end;

procedure TFPesquisaNotaFiscal.BtnLimparClick(Sender: TObject);
begin
  inherited;
 ECodigo.Text:='';
 DBLCBFornecedor.KeyValue:=null;
 RGSituacao.ItemIndex:=3;
 ENotaFiscal.Text:='';
 ESerie.Text:='';
end;

procedure TFPesquisaNotaFiscal.BtnSelecionarClick(Sender: TObject);
begin
  inherited;
  {
  FCadastroNotaFiscal.QNotaFiscal.Close;
  FCadastroNotaFiscal.QNotaFiscal.Open;
  FCadastroNotaFiscal.QNotaFiscal.Locate('NF_Codigo',QPesquisa.fieldByname('NF_Codigo').AsString,[loCaseInsensitive]);
  //FCadastroNotaFiscal.Busca;
  Self.Close;
  }
end;

procedure TFPesquisaNotaFiscal.ENotaFiscalKeyPress(Sender: TObject;
  var Key: Char);
begin
  inherited;
 if not(key in ['0'..'9',#8]) then
  key:=#0;
end;

end.
